﻿// Form sử dụng bảng Catalogue_Course_SchoolYear_Term **
// Form sử dụng bảng Catalogue_Level
// Form sử dụng bảng Catalogue_Department
using System.Windows.Forms;
using DevExpress.XtraBars;
using UniMana.Data;
using UniMana.Utils.Controls;
using UniMana.Utils.Data;
using UniMana.Utils.Helper;
using System;
namespace UniMana.Win.Catalogue
{
    public partial class FrmCategories_SubCourse_SchoolYear_Term : DevExpress.XtraEditors.XtraForm
    {        
        #region[Khai báo biến toàn cục]
        private DBMain m_Catalogue = new DBMain();
        private bds m_bds = new bds();
        private bool Fla = false;
        #endregion 
        #region [sự kiện các nút]
        public void InitControlEvent()
        {
            xucBar.btnAddNew_Click += new xucBar.ButtonClickHandler(btnAddNew_Click);
            xucBar.btnDelete_Click += new xucBar.ButtonClickHandler(btnDelete_Click);
            xucBar.btnEdit_Click += new xucBar.ButtonClickHandler(btnEdit_Click);
            xucBar.btnCancel_Click += new xucBar.ButtonClickHandler(btnCancel_Click);
            xucBar.btnSave_Click += new xucBar.ButtonClickHandler(btnSave_Click);
            xucBar.btnRefresh_Click += new xucBar.ButtonClickHandler(btnRefresh_Click);
        }
        private void btnAddNew_Click(object sender, ItemClickEventArgs e)
        {            
            if (Fla)
            {
                if (m_bds.Count <= 0)
                {
                    Catalogue_Course_SchoolYear_Term dist = new Catalogue_Course_SchoolYear_Term();
                    dist.Catalogue_CourseKey = lueCatalogue_Course.GetIntValue();
                    dist.Catalogue_SchoolYearKey = lueCatalogue_SchoolYear.GetIntValue();
                    dist.Catalogue_TermKey = lueCatalogue_Term.GetIntValue();
                    m_bds.Add(dist);
                    m_bds.MoveLast();

                }
                else
                {
                    MessageBox.Show("Một Học Kỳ Chỉ Được Thiết Lập Thời Gian Một Lần", "Thông Báo");
                }
            }
            else
            {
                MessageBox.Show("Bạn Chưa Chọn Đầy Đủ Đủ Liệu", "Thông Báo");
            }

        }
        private void btnDelete_Click(object sender, ItemClickEventArgs e)
        {
            DialogResult result = new DialogResult();
            result = MessageBox.Show("Bạn Có Chắc Xóa Không", "Thông Báo", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                m_bds.DeleteCurrent();
                btnSave_Click(sender, e);
            }
            else
            {
            }
        }
        private void btnEdit_Click(object sender, ItemClickEventArgs e)
        {
        }
        private void btnCancel_Click(object sender, ItemClickEventArgs e)
        {
            this.Validate();  
            m_bds.CancelEdit();
        }
        private void btnSave_Click(object sender, ItemClickEventArgs e)
        {
            this.Validate();  
            for (int i = 0; i < grvCourse_SchoolYear_Term.RowCount; i++)
            {
                if (this.grvCourse_SchoolYear_Term.GetRowCellValue(
                    i, 
                    gclCatalogue_SchoolYearKey) == null)
                {
                    MessageBox.Show("Chưa nhập dữ liệu đầy đủ");
                    return;
                }
            }
            this.Save(m_bds, m_Catalogue.m_DCSave);
            this.Validate();
        }
        private void btnRefresh_Click(object sender, ItemClickEventArgs e)
        {
        }
        private void lueCatalogue_Course_EditValueChanged(
            object sender, EventArgs e)
        {
            Fla = false;
        }
        private void lueCatalogue_Term_EditValueChanged(
            object sender, EventArgs e)
        {
            Fla = false;
        }
        private void lueCatalogue_SchoolYear_EditValueChanged(
            object sender, EventArgs e)
        {
            Fla = false;
        }
        private void btnSelect_Click(
            object sender, EventArgs e)
        {
            if (lueCatalogue_Course.EditValue != null &&
                lueCatalogue_SchoolYear.EditValue != null &&
                lueCatalogue_Term.EditValue != null)
            {
                Fla = true;
                load();
            }
            else
            {
                MessageBox.Show("Bạn Chưa Chọn Đầy Đủ Đủ Liệu", "Thông Báo");
            }
        }
        private void FrmCatalogue_Course_SchoolYear_Term_Load(object sender, EventArgs e)
        {

        }
        #endregion    
        #region [các hàm sữ dụng]
        private void load()
        {

            int school = -1;
            int term = -1;
            int course = -1;
            course = lueCatalogue_Course.GetIntValue();
            school = lueCatalogue_SchoolYear.GetIntValue();
            term = lueCatalogue_Term.GetIntValue();
            m_bds.DataSource = 
                m_Catalogue.m_DCSave.Catalogue_Course_SchoolYear_Terms.GetBy_course_SchoolYear_Term(
                course, 
                school, 
                term);
            gctCourse_SchoolYear_Term.DataSource = m_bds;
        }
        public void LoadData()
        {
            lueCatalogue_Course.GetData(
                m_Catalogue.m_DCReadOnly.Catalogue_Courses, 
                "Catalogue_CourseKey", 
                "Name");
            lueCatalogue_SchoolYear.GetData(
                m_Catalogue.m_DCReadOnly.Catalogue_SchoolYears, 
                "Catalogue_SchoolYearKey", 
                "Name");
            lueCatalogue_Term.GetData(
                m_Catalogue.m_DCReadOnly.Catalogue_Terms, 
                "Catalogue_TermKey", 
                "Name");
            rluCatalogue_Course.GetData(
                m_Catalogue.m_DCReadOnly.Catalogue_Courses, 
                "Catalogue_CourseKey", 
                "Name");
            rluCatalogue_SchoolYear.GetData(
                m_Catalogue.m_DCReadOnly.Catalogue_SchoolYears, 
                "Catalogue_SchoolYearKey", 
                "Name");
            rluCatalogue_Term.GetData(
                m_Catalogue.m_DCReadOnly.Catalogue_Terms, 
                "Catalogue_TermKey", 
                "Name");
            rluCatalogue_TermSchoolYear.GetData(
                m_Catalogue.m_DCReadOnly.Catalogue_TermSchoolYears, 
                "Catalogue_TermSchoolYearKey", 
                "Name");
        }
        public FrmCategories_SubCourse_SchoolYear_Term()
        {
            InitializeComponent();
            LoadData();
            InitControlEvent();
        }
        #endregion
    }
}